package dao;

import java.util.List;

import model.Customers;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import idao.ICustomersDAO;

@Repository
public class CustomersDAO implements ICustomersDAO {

	@Autowired
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public CustomersDAO() {
		// TODO Auto-generated constructor stub
	}

	public boolean insert(Customers customers) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().save(customers);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	public boolean update(Customers customers) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().update(customers);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	public boolean delete(Customers customers) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().delete(customers);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	public Customers getCustomerByUser(String user) {
		// TODO Auto-generated method stub
		try {
			Customers customers = (Customers) sessionFactory
					.getCurrentSession().get(Customers.class, user);
			return customers;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Customers> getAll() {
		// TODO Auto-generated method stub
		try {
			Query query = sessionFactory.getCurrentSession().createQuery(
					"from Customers");
			return query.list();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

}
